package org.leetcode.easy.leetcode349;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;

public class Solution
{

    public int[] intersection(int[] nums1, int[] nums2) {

        HashMap<Integer, Integer> map1 = new HashMap<>();

        for (int num1:nums1) {
            if (map1.containsKey(num1))
            {
                Integer value = map1.get(num1);
                value =value+1;
                map1.put(num1,value);
            }else {
                map1.put(num1,1);
            }
        }

        HashSet<Integer> hashset = new HashSet<>();

        for (int num2:nums2) {
            if (map1.containsKey(num2))
            {
                hashset.add(num2);
            }
        }

        int []result = new int[hashset.size()];

        int index =0;
        for (int num:hashset) {
            result[index]=num;
            index++;
        }

        return result;
    }

    public static void main(String[] args) {
        int [] nums1={1,2,2,1};
        int [] nums2={2,2};

        Solution solution = new Solution();
        int[] result = solution.intersection(nums1, nums2);
    }
}
